package com.kyxx.topics.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kyxx.topics.BaseActivity;
import com.kyxx.topics.R;
import com.kyxx.topics.bean.CouponBean;
import com.kyxx.topics.net.UrlUtils;
import com.kyxx.topics.utils.ScreenSizeUtils;
import com.kyxx.topics.utils.StatusBarCompat;
import com.kyxx.topics.view.CircleImageView;
import com.kyxx.topics.zxing.encode.EncodingHandler;

public class DetailYouHuiQuanActivity extends BaseActivity {
    private RelativeLayout rl_bg;
    private ImageView iv_back;
    private CircleImageView civ_huiyuanka;
    private TextView tv_name;
    private TextView tv_count;
    private TextView tv_time;
    private TextView tv_content;
    private View v_0;
    private View v_1;
    private ImageView iv_code;
    private TextView tv_code;
    private CouponBean couponBean = new CouponBean();
    private String[] colors = new String[3];

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_detail_you_hui_quan);
    }

    @Override
    protected void findViewById() {
        rl_bg = (RelativeLayout) findViewById(R.id.rl_bg);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        civ_huiyuanka = (CircleImageView) findViewById(R.id.civ_huiyuanka);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_count = (TextView) findViewById(R.id.tv_count);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_content = (TextView) findViewById(R.id.tv_content);
        v_0 = findViewById(R.id.v_0);
        v_1 = findViewById(R.id.v_1);
        iv_code = (ImageView) findViewById(R.id.iv_code);
        tv_code = (TextView) findViewById(R.id.tv_code);
    }

    @Override
    protected void setListener() {
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void processLogic() {
        couponBean = (CouponBean) getIntent().getSerializableExtra("bean");
        tv_code.setText(getFormatedText(couponBean.getCode()));
        iv_code.setImageBitmap(createBarCode(couponBean.getCode()));
//        try {
//            colors = couponBean.getColor().split(",");
//        } catch (Exception e) {
//            e.printStackTrace();
//            colors[0] = "242";
//            colors[1] = "178";
//            colors[2] = "28";
//        }
        int b = new java.math.BigInteger(couponBean.getColor(), 16).intValue();
        GradientDrawable drawable0 = (GradientDrawable) v_0.getBackground();
        drawable0.setColor(b);
        GradientDrawable drawable1 = (GradientDrawable) v_1.getBackground();
        drawable1.setColor(b);
        rl_bg.setBackgroundColor(b);
        imageLoader.displayImage(UrlUtils.BASEHTTP + couponBean.getImage(), civ_huiyuanka, options);
        tv_name.setText(couponBean.getShop_name());
        if (couponBean.getType() == 1) {//体验券
            tv_count.setText("剩余" + couponBean.getTimes_leave() + "次\n" + "共" + couponBean.getTimes_total() + "次");
        } else if (couponBean.getType() == 2) {//抵扣券
            tv_count.setText("满" + couponBean.getJianmian() + "元\n" + "抵扣" + couponBean.getDikou() + "元");
        }
        tv_time.setText("有效期" + couponBean.getStarttime() + "至\n" + couponBean.getEndtime());
        tv_content.setText(couponBean.getTitle());
        StatusBarCompat.compat(this, b);
    }

    private Bitmap createBarCode(String key) {
        Bitmap qrCode = null;
        try {
            qrCode = EncodingHandler.createBarCode(key, (int) (ScreenSizeUtils.getInstance(context).getScreenWidth() * 0.7f), (int) (ScreenSizeUtils.getInstance(context).getScreenHeight() * 0.17f));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return qrCode;
    }

    /**
     * 获取每四位分隔的银行卡号码
     *
     * @param textWithSpace
     */
    private String getFormatedText(String textWithSpace) {
        if (textWithSpace == null) {
            return "";
        }
        String arrs[] = textWithSpace.split("");
        int len = arrs.length;
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i < len; i++) {
            sb.append(arrs[i]);
            if (i % 4 == 0) {
                sb.append(" ");
            }
        }
        if (len % 4 == 1) {
            sb.append(" ");
        }
        return sb.toString().trim();//机智的trim掉可能是手机号的空格
    }

    public void setWindowStatusBarColor(Activity activity, int colorResId) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = activity.getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.setStatusBarColor(activity.getResources().getColor(colorResId));

                //底部导航栏
                //window.setNavigationBarColor(activity.getResources().getColor(colorResId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setStatusBar() {
        int sdk = Build.VERSION.SDK_INT;
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        if (sdk >= Build.VERSION_CODES.LOLLIPOP) {
            int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            // 设置透明状态栏
            if ((params.flags & bits) == 0) {
                params.flags |= bits;
                window.setAttributes(params);
            }
        }
    }
}
